/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.server;

import java.io.*;
import java.sql.Timestamp;

public class ServerUtil {

    public static int EXPIRE_HOURS = 24 * 10;
    
    public static boolean IS_TEST_DRIVE = false;

    public static String[] MONTH_ABBRS_UC = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT",
            "NOV", "DEC" };

    public static int getMonthValue(String abbr) {
        abbr = abbr.toUpperCase();
        int month = -1;
        for (int i = 0; i < MONTH_ABBRS_UC.length; i++) {
            if (abbr.equals(MONTH_ABBRS_UC[i])) {
                month = i + 1;
                break;
            }
        }
        return month;
    }

    public static String removeUnicode(String str) {
        /*
         * String temp = str; String temp2 = str;
         * 
         * while (true) { for (int i = 0; i < toReplace.length; i++) { temp =
         * temp.replace(toReplace[i], ' '); } if (temp.equals(temp2)) return
         * temp; temp2 = temp; }
         */
        StringBuilder newStr = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            int type = Character.getType(str.charAt(i));
            if ((type == 0) || (type == 15))
                newStr.append(" ");
            else
                newStr.append(str.charAt(i));
        }
        return newStr.toString();
    }

    /***************************************************************************
     * Runs a UNIX command and waits for completion of the command. Properly
     * removes file descriptors, avoiding several JRE bugs.
     * 
     * @param command
     *            The UNIX shell command to run
     * @return The value returned from the command executed.
     */
    public static int execAndWait(String command) {
        try {
            Process p = Runtime.getRuntime().exec(command);
            int val = p.waitFor();
            try {
                p.getInputStream().close();
                p.getOutputStream().close();
                p.getErrorStream().close();
                p.destroy();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }

            return val;
        } catch (Exception E) {
            E.printStackTrace();
        }
        return -1;
    }

    public static String millisToTS(long millis) {
        /*
         * String ts = ""; GregorianCalendar cal = new GregorianCalendar();
         * cal.setTimeInMillis(millis); String ms =
         * cal.get(Calendar.MILLISECOND) + ""; while (ms.length() < 3) ms = "0" +
         * ms;
         * 
         * ts = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) +
         * "-" + cal.get(Calendar.DAY_OF_MONTH) + " " + cal.get(Calendar.HOUR) +
         * ":" + cal.get(Calendar.MINUTE) + "-" + cal.get(Calendar.SECOND) + "." +
         * ms;
         */
        Timestamp ts = new Timestamp(millis);

        return ts.toString();

    }



}
